/*
 * @Author: zlc
 * @Date: 2022-07-26 16:25:31
 * @LastEditTime: 2022-07-28 18:08:50
 * @LastEditors: zlc
 * @Description: 分配manifest.json
 * @FilePath: \teachernotice\modifyManifest.js
 */
const fs=require('fs')
const {JXY_PROJECT_ENUM,YXB_PROJECT_ENUM,USE_PROJECT_ENUM} = require('./project/projectEnum.js')
const manifestPath = process.env.UNI_INPUT_DIR + '/manifest.json'
let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' })
function replaceManifest(path, value) {
  const arr = path.split('.')
  const len = arr.length
  const lastItem = arr[len - 1]

  let i = 0
  let ManifestArr = Manifest.split(/\n/)

  for (let index = 0; index < ManifestArr.length; index++) {
    const item = ManifestArr[index]
    if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
    if (i === len) {
      const hasComma = /,/.test(item)
      ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
      break;
    }
  }

  Manifest = ManifestArr.join('\n')
}
function deCode(code){
  code = unescape(code);       
  var c=String.fromCharCode(code.charCodeAt(0)-code.length);       
  for(var i=1;i<code.length;i++){       
      c+=String.fromCharCode(code.charCodeAt(i)-c.charCodeAt(i-1));
  }       
  return c;
} 
if ((process.env.UNI_SCRIPT?process.env.UNI_SCRIPT:USE_PROJECT_ENUM)===JXY_PROJECT_ENUM){
    replaceManifest('mp-weixin.appid', `"${deCode('%89%EF%DE%99%95%94fklkk%97%9Bi%95%9Cm%97')}"`)
}else {
	
    replaceManifest('mp-weixin.appid', `"${deCode('%89%EF%D9%C7%9B%9B%C9%93%95%96%94%94%92%C2%C5%9C%9D%98')}"`)
   
}

fs.writeFileSync(manifestPath, Manifest, {
  "flag": "w"
})